package chat_server;

// -----------------------------------------------------------------------------
// Importanweisungen
import for_all.Message;
import for_all.PopUp;
import for_all.Room;
import for_all.User;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Hashtable;


public class ManageRooms implements Serializable {
    
    // -------------------------------------------------------------------------
    // Variablendeklarationen
    private Hashtable<String,Room> rooms;
    private ArrayList<String> roomNames;
    private ServerGui serverGUI;
    
    
    // -------------------------------------------------------------------------
    // Konstruktor
    public ManageRooms(ServerGui serverGUI) {
        rooms = new Hashtable<>();
        roomNames = new ArrayList<>();
        this.serverGUI = serverGUI;
    }
    
    
    // -------------------------------------------------------------------------
    // Raum holen
    public Room getRoom(String roomName) {
        return rooms.get(roomName);
    }
    
    
    // -------------------------------------------------------------------------
    // neuen Raum erstellen
    public void createRoom(String name) {
        rooms.put(name, new Room(name));
        roomNames.add(name);
    }
    
    
    // -------------------------------------------------------------------------
    // Räume erstellen
    public void createRooms(ArrayList<String> Names) {
        for(String temp: Names) {
            rooms.put(temp, new Room(temp));
            roomNames.add(temp);
        }
    }
    
    
    // -------------------------------------------------------------------------
    //  Liefert eine Liste mit allen Raumnamen
    public ArrayList<String> getRoomNames() {
        return roomNames;
    }
    
    
    // -------------------------------------------------------------------------
    // get Anzahl an User im Raum
    public int getUserCount(String roomName) {
        return rooms.get(roomName).getNumberOfUsers();
    }
    
    
    // -------------------------------------------------------------------------
    // Liste mit Namen der User im Raum
    public ArrayList<String> getUserNames(String roomName) {
        return rooms.get(roomName).getUserNames();
    }
    
    
    // -------------------------------------------------------------------------
    // Raum Löschen
    public void deleteRoom(String name) {
        rooms.remove(name);
        roomNames.remove(name);
    }
    
    
    // -------------------------------------------------------------------------
    // Raumnamen ändern
    public void changeRoomName(String oldName, String newName) {
        Room temp = rooms.get(oldName);
        temp.changeRoomName(newName);
        rooms.remove(oldName);
        rooms.put(newName, temp);
        roomNames.remove(oldName);
        roomNames.add(newName);
    }
    
    
    // -------------------------------------------------------------------------
    // Sendet eine Nachricht an alle User im Raum
    public void sendMessageToAllUsersInRoom(String roomName, Message msg) {
        rooms.get(roomName).sendMessageToAllUSers(msg);
        serverGUI.setMessage(msg);
    }
    
    
    // -------------------------------------------------------------------------
    // sendet eine Nachricht an alle User im Raum, mit Ausnahme von User "without"
    public void sendMessageToAllUserInRoomWithout(String roomName, Message msg, String without) {
        rooms.get(roomName).sendMessageToAllUSersWithout(msg, without);
        serverGUI.setMessage(msg);
    }
    
    
    // -------------------------------------------------------------------------
    // User tritt dem Raum bei
    public void addUser(String roomName, User user) {
        rooms.get(roomName).addUser(user);
        user.setRoom(roomName);
        this.serverGUI.setUser(user, true);
        serverGUI.InkrementUserCountInRoom(roomName);
    }
    
    
    // -------------------------------------------------------------------------
    // User verlässt den Raum
    public void removeUser(String roomName, User user) {
        if (!"--null--".equals(roomName)) {
            rooms.get(roomName).removeUser(user);
            serverGUI.DekrementUserCountInRoom(roomName);
        }
    }
    
    
    // -------------------------------------------------------------------------
    // Räume exportieren
    public void roomExport() {
        FileWriter writer = null;
        try {
            writer = new FileWriter(System.getProperty("user.dir")+"\\Data\\rooms.ini", false);
            for(String name: roomNames) {
                writer.write(name + "\r\n");
            }
        } catch (IOException ex) {
            PopUp fehler = new PopUp(serverGUI, "IOException : " + ex.getMessage(), "Fehler in RoomExport", PopUp.MessageTyp.Error);
        } finally {
            try {
                writer.close();
            } catch (IOException ex) {
                PopUp fehler = new PopUp(serverGUI, "Fehler beim Schließen des FileWriter: " + ex.getMessage(), "Fehler in RoomExport", PopUp.MessageTyp.Error);
            }
        }
    }
    
    
    // -------------------------------------------------------------------------
    // Räume importieren
    public boolean roomImport(boolean showNotes){
        File datei = new File(System.getProperty("user.dir")+"\\Data\\rooms.ini");
        boolean returnStatement = false;
        
        if(!datei.exists()) { //Falls Datei noch nicht existiert
            if(showNotes) {
                PopUp fehler = new PopUp(serverGUI, "Es wurden keine Räume geladen, da keine rooms.ini vorhanden ist");
            }
            return false;
        }
        
        FileReader reader;
        BufferedReader textReader = null;
        
        try {
            reader = new FileReader(datei.getPath());
            textReader = new BufferedReader(reader);
            
            ArrayList<String> names = new ArrayList<>();
            String raumName = textReader.readLine();
            while(raumName != null) {
                returnStatement = true;
                createRoom(raumName);
                names.add(raumName);
                
                raumName = textReader.readLine();
            }
            serverGUI.addRoomNames(names);
            
        } catch (FileNotFoundException ex) {
            PopUp fehler = new PopUp(serverGUI, "FileNotFoundException : " + ex.getMessage(), "Fehler in RoomImport", PopUp.MessageTyp.Error);
        } catch (IOException ex) {
            PopUp fehler = new PopUp(serverGUI, "IOException : " + ex.getMessage(), "Fehler in RoomImport", PopUp.MessageTyp.Error);
        } finally {
            try {
                textReader.close();
            } catch (IOException ex) {
                PopUp fehler = new PopUp(serverGUI, "Stream konnte nicht geschlossen werden: " + ex.getMessage(), "Fehler in RoomImport", PopUp.MessageTyp.Error);
            }
        }
        return returnStatement;
    }
    
   
    // -------------------------------------------------------------------------
    // liefert zurück ob roomName enthalten ist
    public boolean containsRoom(String roomName) {
        return rooms.containsKey(roomName);
    }

    public ServerGui getGui() {
        return serverGUI;
    }
}